﻿using System;
using System.Collections.Generic;
using System.Linq;
using HYCodeBase.Algorithm.Octree;
using HYCodeBase.Algorithm.Octree.Data;
using NetTopologySuite.Index.Quadtree;

namespace HYCodeBase.Algorithm
{
	public static class TreeSearchUtility
	{
		public static Dictionary<T, List<T>> NearSearch<T>(List<T> input) where T : AABBDATA
		{
			Quadtree<T> quadtree = new Quadtree<T>();
			foreach (T t in input)
			{
				quadtree.Insert(t.AABB, t);
			}
			Dictionary<T, List<T>> dictionary = new Dictionary<T, List<T>>();
			foreach (T t2 in quadtree.QueryAll())
			{
				IList<T> collection = quadtree.Query(t2.AABB);
				if (!dictionary.ContainsKey(t2))
				{
					dictionary.Add(t2, new List<T>());
				}
				dictionary[t2].AddRange(collection);
			}
			return dictionary;
		}

		public static Dictionary<T, List<T>> NearSearchOctree<T>(List<T> input) where T : AABBDATAThreeD
		{
			Dictionary<T, List<T>> dictionary = new Dictionary<T, List<T>>();
			BoundsOctree<T> boundsOctree = new BoundsOctree<T>(15f, Point.Zero, 1f, 1f);
			input = input.Distinct<T>().ToList<T>();
			foreach (T t in input)
			{
				boundsOctree.Add(t, t.UseBox);
			}
			foreach (T t2 in input)
			{
				List<T> list = new List<T>();
				boundsOctree.GetColliding(list, t2.UseBox);
				dictionary.Add(t2, list);
			}
			return dictionary;
		}
	}
}
